package com.crux.ribbon;

import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.loadbalancer.core.DelegatingServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ApiVersionServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier {
    private ServiceInstanceListSupplier delegate;

    public ApiVersionServiceInstanceListSupplier(ServiceInstanceListSupplier delegate) {
        super(delegate);
        this.delegate = delegate;
    }

    @Override
    public Flux<List<ServiceInstance>> get(Request request) {
        String serviceVersions = getHeader("x-service-versions");
//        return delegate.get(request).map(filterByVersion);
        return delegate.get(request).map(data -> filterByVersion(data,serviceVersions));
    }

    @Override
    public Flux<List<ServiceInstance>> get() {
        String serviceVersions = getHeader("x-service-versions");
        return delegate.get().map(data -> filterByVersion(data,serviceVersions));
    }

    private List<ServiceInstance> filterByVersion(List<ServiceInstance> serviceInstances,String serviceVersions) {
        String serviceId = delegate.getServiceId();
//        String serviceVersions = RequestUtil.getHeader("x-service-versions");
        if (StringUtils.isNotBlank(serviceVersions)) {
            String[] serviceVersionArray = serviceVersions.split(";");
            String matchServiceVersion = null;
            for (String serviceVersion : serviceVersionArray) {
                String[] arr = serviceVersion.split("=");
                String headerServiceId = arr[0];
                String headerServiceVersion = arr[1];
                if (headerServiceId.equals(serviceId)) {
                    matchServiceVersion = headerServiceVersion;
                    break;
                }
            }
            if (matchServiceVersion != null) {
                final String finalMatchServiceVersion = matchServiceVersion;
//                List<ServiceInstance> result = new ArrayList<>();
//                for (ServiceInstance serviceInstance : serviceInstances) {
//                    String optionalServiceId = serviceInstance.getServiceId();
//                    System.out.println("optionalServiceId is: " + optionalServiceId);
//                    String version = serviceInstance.getMetadata().get("instance-version");
//                    System.out.println("version is : " + version);
//                    if(serviceId.equals(serviceInstance.getServiceId()) && finalMatchServiceVersion.equals(version)){
//                        result.add(serviceInstance);
//                    }
//                }
//                return result;
                return serviceInstances.stream().filter((serviceInstance -> serviceInstance.getServiceId().equalsIgnoreCase(serviceId) && finalMatchServiceVersion.equals(serviceInstance.getMetadata().get("instance-version")))).collect(Collectors.toList());
            }
        }
        return serviceInstances;
    }

    private String getHeader(String name) {
        return RequestUtil.getHeader(name);
    }
}
